package com.example.skycode5.servlet;

import com.example.skycode5.Bean.NewsBean;
import com.example.skycode5.dao.NewsDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/newsAdd.jsp")
public class AdminUpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String newsTitle = req.getParameter("newsTitle");
        String newsContent = req.getParameter("upp_NewsTitle");
        int newsId =Integer.parseInt(req.getParameter("newsId"));
        NewsBean newsBean = new NewsBean();//实例化新闻对象
        newsBean.setNewsid(newsId);
        newsBean.setNewstitle(newsTitle);;//设置新闻标题
        newsBean.setNewscontent(newsContent);//设置新闻内容
        newsBean.setAdminName("admin");
        NewsDao newsDao = new NewsDao();//实例化新闻DAO
        int result = newsDao.update(newsBean);
        if (result>0){
            resp.sendRedirect("news.jsp");//参数是相对网址                               3
        }else {
            req.setAttribute("msg", "更新失败");//错误页面显示内容
            req.setAttribute("url" , "news.jsp");//显示错误信息后要跳转的页面
            req.getRequestDispatcher("WEB-INF/error.jsp").forward(req,resp);
        }
    }
}